package servlets;

import java.io.IOException;
import java.util.List;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import vo.ArticuloVO;
import vo.VentaVO;
import facade.BD;

/**
 * Servlet implementation class Acciones
 */
@WebServlet("/Acciones")
public class Acciones extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public Acciones() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String accion = request.getParameter("accion");
		BD bd = BD.getInstance();
		String jspPage = "";
		
		
		if (accion.equals("BusquedaArticulos")){
			List<ArticuloVO> articulos = bd.getArticulos();
			request.setAttribute("articulos", articulos);
    		
    		jspPage = "/BusquedaArticulos.jsp";
		}
		if (accion.equals("ListarOrdenes")){

			List<VentaVO> ventas = bd.getVentas();
			request.setAttribute("ventas", ventas);
			jspPage = "/ListaOrdenes.jsp";

		}
		if (accion.equals("VistaCarrito")){
			jspPage = "/VistaCarrito.jsp";
		}
		if (accion.equals("CerrarSesion")){
			request.setAttribute("errorMessage", "Muchas gracias por su visita. Vuelva pronto.");
    		jspPage = "/InicioDeSesion.jsp";
		}	
		
		request.removeAttribute("accion");
  	dispatch(jspPage, request, response);
    	
    }

	protected void dispatch(String jsp, HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		if (jsp != null) {
			RequestDispatcher rd = request.getRequestDispatcher(jsp);
			rd.forward(request, response);
		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

}
